package Relatorios;

import hibernate.dao.HibernateConnection;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import skin.LookAndFeelSelector;
import factory.FabricaDeFrame;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.demo.DateChooserPanel;

import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JScrollPane;

public class FrmHistorico extends JFrame {

	private JPanel contentPane;
	private JLabel lblSelecioneOCliente;
	private JLabel lblDataInicial;
	private JLabel lblDataFinal;
	private JDateChooser dataInicial;
	private JDateChooser dataFinal;
	private JScrollPane scrollPaneHistorico;
	private JTable tbHistorico;
	private JComboBox cbCliente;
	private JButton btnGerarRelatorio;
	private JSeparator separator;
	private JButton btnExcel;
	private JButton btnPdf;
	private CtrlRelatorio ctrl = new CtrlRelatorio();

	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HibernateConnection.init();
					LookAndFeelSelector.lookAndFeelOS();
					FrmHistorico frame = (FrmHistorico) FabricaDeFrame.getInstanceHistorico();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrmHistorico() {
		setTitle("Historico de Clientes");
		setIconImage(Toolkit.getDefaultToolkit().getImage("src/Repositorio/icon.png"));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 518, 451);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setLocationRelativeTo(null);
		contentPane.setLayout(null);
		
		lblSelecioneOCliente = new JLabel("Selecione o cliente :");
		lblSelecioneOCliente.setBounds(17, 22, 118, 16);
		contentPane.add(lblSelecioneOCliente);
		
		cbCliente = new JComboBox();
		cbCliente.setBounds(160, 17, 222, 26);
		contentPane.add(cbCliente);
		
		lblDataInicial = new JLabel("Data Inicial: ");
		lblDataInicial.setBounds(17, 54, 81, 16);
		contentPane.add(lblDataInicial);
		
		dataInicial =  new JDateChooser();
		dataInicial.setBounds(161, 50, 120, 25);
		contentPane.add(dataInicial);
		
		lblDataFinal = new JLabel("Data Final:");
		lblDataFinal.setBounds(17, 88, 81, 16);
		contentPane.add(lblDataFinal);
		
		dataFinal =  new JDateChooser();
		dataFinal.setBounds(161, 84, 120, 25);
		contentPane.add(dataFinal);
	
		btnGerarRelatorio = new JButton("Gerar");
		btnGerarRelatorio.setBounds(371, 82, 90, 28);
		contentPane.add(btnGerarRelatorio);
		btnGerarRelatorio.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ctrl.carregaTableHistorico(dataInicial, dataFinal, cbCliente, tbHistorico);
				
			}
		});
		
		separator = new JSeparator();
		separator.setBounds(6, 125, 491, 8);
		contentPane.add(separator);
		
		scrollPaneHistorico = new JScrollPane();
		scrollPaneHistorico.setBounds(17, 144, 462, 200);
		contentPane.add(scrollPaneHistorico);
		
		
		tbHistorico = new JTable();
		tbHistorico.setBounds(17, 145, 462, 200);
		scrollPaneHistorico.setViewportView(tbHistorico);
		
		
		btnExcel = new JButton();
		btnExcel.setBounds(352, 356, 50, 50);
		btnExcel.setToolTipText("Exporta para Excel");
		btnExcel.setIcon(new ImageIcon("src/Repositorio/btn_excel.png"));
		contentPane.add(btnExcel);
		btnExcel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ctrl.exportaExcelHistorico(dataInicial, dataFinal, cbCliente);
				
			}
		});
		
		btnPdf = new JButton("");
		btnPdf.setBounds(429, 356, 50, 50);
		btnPdf.setToolTipText("Exporta para PDF");
		btnPdf.setIcon(new ImageIcon("src/Repositorio/btn_pdf.png"));
		contentPane.add(btnPdf);
		
		
		
		this.addWindowListener(new WindowAdapter() {
			
			public void windowOpened(WindowEvent e){
				ctrl.carregarCbCLiente(cbCliente);
			}
		});
		
	}
}
